package logic

import (
	"context"
	"fmt"
	"math/rand"
	"time"
	"zhihu/models"
	"zhihu/zhihu_rpc/internal/svc"
	"zhihu/zhihu_rpc/zhihu_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsLogic {
	return &SmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SmsLogic) Sms(in *zhihu_rpc.SmsRequest) (*zhihu_rpc.SmsResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("%s:phone:%s", in.From, in.Phone)
	count, _ := models.RDB.Get(models.Ctx, key+"count").Result()
	if count != "" {
		return nil, fmt.Errorf(count)
	}
	Code := fmt.Sprintf("%d", rand.Intn(9000)+1000)
	//sms, err := pkg.Sms(Code)
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}
	models.RDB.Set(models.Ctx, key, Code, 3*time.Minute)
	models.RDB.Set(models.Ctx, key+"count", "请一分钟后再试", 1*time.Minute)
	return &zhihu_rpc.SmsResponse{}, nil
}
